package in.muton.edushare.components.forms.usersettings;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.processing.objects.UserDataHandler;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;

import java.util.HashMap;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.vo.Users;

public class PrivacyForm extends Panel {

	private static final long serialVersionUID = -4839642137122687005L;

	public PrivacyForm(String id, final IModel<Users> model) {
		super(id, model);
		boolean isStudent = new UserDataHandler(model).isStudent();
		auth = new Authentication(model.getObject(), EduShareSession.get()
				.getLoggedInUser());

		HashMap<String, String> permissionSettings = new HashMap<String, String>();
		permissionSettings.put("People from my university", "qd");
		permissionSettings.put("People from my department", "qe");
		permissionSettings.put("People from my courses", "qf");
		permissionSettings.put("Everyone", "qa");
		
		final GenericRenderer<String> gr1 = new GenericRenderer<String>(
				permissionSettings, auth.getSetting(Authentication.MSG_SND));
		final GenericRenderer<String> gr2 = new GenericRenderer<String>(
				permissionSettings, auth.getSetting(Authentication.CAL_NEW));
		final GenericRenderer<String> gr3 = new GenericRenderer<String>(
				permissionSettings, auth.getSetting(Authentication.FILE_UP));
		
		
		Form<Void> f = new Form<Void>("privacyForm") {
			@Override
			public void onSubmit() {
				auth.changeSetting(Authentication.MSG_SND, gr1.getSelection());
				auth.changeSetting(Authentication.CAL_NEW, gr2.getSelection());
				auth.changeSetting(Authentication.FILE_UP, gr3.getSelection());
				model.getObject().setSettings(auth.getString());
				EduShareSession.get().getSessionORMInterface().commitAllChanges();
			}

			private static final long serialVersionUID = 5773965500722555111L;
		};

		add(f);

		f.add(new WebMarkupContainer("isStudent2").setVisible(isStudent));
		f.add(new WebMarkupContainer("isStudent1").setVisible(isStudent));
		
		f.add(new DropDownChoice<String>("mHPSelect",
				new PropertyModel<String>(gr1, "selectedValue"), gr1
						.getOptions(), gr1));

		f.add(new DropDownChoice<String>("sHPSelect",
				new PropertyModel<String>(gr2, "selectedValue"), gr2
						.getOptions(), gr2));

		f.add(new DropDownChoice<String>("filePSelect",
				new PropertyModel<String>(gr3, "selectedValue"), gr3
						.getOptions(), gr3));
	}
	@Override
	public boolean isVisible(){
		return false;
	}

	private Authentication auth = null;
}
